<?php
namespace Common\Model;
use Think\Model;
// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------
class SyncLoginModel extends Model{
    
    /**
     * 数据库表名
     * @author ubten <462193409@qq.com>
     */
    protected $tableName = 'sync_login';
    
    /**
     * 新增SNS登录账号
     */
    public function update($id){
        $token = session('token');
        $user_sns_info = session('user_sns_info');
        $data['uid'] = $id;
        $data['type'] = $user_sns_info['type'];
        if($user_sns_info['type'] == "WEIXIN"){
            $data['openid'] = $token['unionid'];
        }else{
            $data['openid'] = $token['openid'];
        }
        $data['access_token'] = $token['access_token'];
        $data['refresh_token'] = $token['refresh_token'];
        $data['ctime'] = NOW_TIME;
        $data = $this->create($data);
        return $this->add($data);
    }

    /**
     * 根据openid等参数查找同步登录表中的用户信息
     */
    public function getUserByOpenidAndType($openid, $type){
        $condition = array(
            'openid' => $openid,
            'type' => $type,
        );
        return $this->where($condition)->find();
    }

    /**
     * 更新Token
     */
    public function updateTokenByTokenAndType($token, $type){
        $condition = array(
            'openid' => $token['openid'],
            'type' => $type,
        );
        $data['access_token'] = $token['access_token'];
        $data['refresh_token'] = $token['refresh_token'];
        $data['utime'] = NOW_TIME;
        if($this->where($condition)->save($data)){
            return true;
        }
    }
    
    
}